// import {getCarPath} from "@/util/nodeUtil";

export default function (G6){
    G6.registerBehavior('dragAnchor', {

        getDefaultCfg(){
            return{
                origin: null,
                target: null
            }
        },

        getEvents(){
            return {
                'anchor:dragstart': 'onDragstart',
                'anchor:drag': 'onDrag',
                'anchor:dragend': 'onDragEnd',
                'anchor:dragenter': 'onDragEnter',
                'anchor:dragleave': 'onDragLeave',
            }
        },

        onDragstart(e){
            if (!this.shouldBegin.call(this, e)){
                return;
            }
            this.target = e.item;
            this.origin = {x: e.x, y:e.y};
        },

        onDrag(e){
            const groups = e.item.getContainer().getParent().getChildren();
            const x = e.x - this.origin.x;
            const y = e.y - this.origin.y;
            const delegateShape = groups.find(item => item.cfg.name === 'masking-shape');
            const anchors = groups.filter(item => item.cfg.id);
            const nodes = groups.filter(item => item.cfg.name && item.cfg.name !== 'masking-shape');
            this._setPoint(delegateShape);
            this._setNodeSize(e, nodes, {x: x, y: y});
            if(delegateShape){
                this._setDelegateShape(delegateShape, e);
                this._setAnchorsPosition(anchors, delegateShape);
            }else{
                this._setLineAnchorsPosition();
            }
        },

        /**
         * 控制遮罩大小
         * @param item
         * @param e
         * @private
         */
        _setDelegateShape(item, e){
            const x = e.x - this.origin.x + this.point.x;
            const y = e.y - this.origin.y + this.point.y;
            item.attr({
                width: x,
                height: y,
            });
            this.origin = {x:e.x, y: e.y};
            this.point = {x, y};
        },

        /**
         * 设置node大小
         * @param e
         * @param nodes
         * @private
         */
        _setNodeSize(e, nodes){
            nodes.forEach((item) => {
                const clazz = item.getParent().get("item").getModel().clazz;
                switch (clazz){
                    case 'line': this._setLine(e); break;
                    case 'twoBessel': this._setTwoBessel(e); break;
                    case 'threeBessel': this._setThreeBessel(e); break;
                    case 'four': this._setRect(e); break;
                    case 'rect': this._setRect(e); break;
                    case 'car': this._setRect(e); break;
                    case 'location': this._setRect(e); break;
                    case 'carLocation': this._setRect(e); break;
                    case 'charging': this._setRect(e); break;
                    default:
                        break;
                }
            });
        },

        /**
         * 更新矩形
         * @param e
         * @private
         */
        _setRect(e){
            const x = e.x - this.origin.x + this.point.x;
            const y = e.y - this.origin.y + this.point.y;
            let item = this.graph.findById(e.item.getContainer().getParent().cfg.id);
            this.graph.updateItem(item, {size: [x, y]});
            this._resetPoint(e, {x: x, y: y});
        },

        /**
         * 拖动三次贝塞尔曲线
         * @param e
         * @private
         */
        _setThreeBessel(e){
            const x = e.x - this.origin.x + this.point.x;
            const y = e.y - this.origin.y + this.point.y;
            const index = this.target._cfg.index;
            const groups = e.item.getContainer().getParent().getChildren();
            const anchors = groups.filter(item => item.cfg.id);
            const origin = index === 0 ? {x: x, y: y} : {x:anchors[0].cfg.item.getModel().style.x, y: anchors[0].cfg.item.getModel().style.y };
            const control1 = index === 1 ? {x: x, y: y} : {x:anchors[1].cfg.item.getModel().style.x, y: anchors[1].cfg.item.getModel().style.y };
            const control2 = index === 2 ? {x: x, y: y} : {x:anchors[2].cfg.item.getModel().style.x, y: anchors[2].cfg.item.getModel().style.y };
            const target = index === 3 ? {x: x, y: y} : {x:anchors[3].cfg.item.getModel().style.x, y: anchors[3].cfg.item.getModel().style.y };
            let item = this.graph.findById(this.target.getContainer().getParent().cfg.id);
            this.graph.updateItem(item, {origin: origin, control1: control1,control2: control2, target: target});
            this._resetPoint(e, {x: x, y: y});
        },

        /**
         * 拖动锚点改变二次贝尔曲线长度
         * @param e
         * @private
         */
        _setTwoBessel(e){
            const x = e.x - this.origin.x + this.point.x;
            const y = e.y - this.origin.y + this.point.y;
            const index = this.target._cfg.index;
            const groups = e.item.getContainer().getParent().getChildren();
            const anchors = groups.filter(item => item.cfg.id);
            const origin = index === 0 ? {x: x, y: y} : {x:anchors[0].cfg.item.getModel().style.x, y: anchors[0].cfg.item.getModel().style.y };
            const control = index === 1 ? {x: x, y: y} : {x:anchors[1].cfg.item.getModel().style.x, y: anchors[1].cfg.item.getModel().style.y };
            const target = index === 2 ? {x: x, y: y} : {x:anchors[2].cfg.item.getModel().style.x, y: anchors[2].cfg.item.getModel().style.y };
            let item = this.graph.findById(this.target.getContainer().getParent().cfg.id);
            this.graph.updateItem(item, {origin: origin, control: control, target: target});
            this._resetPoint(e, {x: x, y: y});
        },

        /**
         * 拖动锚点改变线长
         * @param e
         * @private
         */
        _setLine(e){
            const x = e.x - this.origin.x + this.point.x;
            const y = e.y - this.origin.y + this.point.y;
            const index = this.target._cfg.index;
            let item = this.graph.findById(this.target.getContainer().getParent().cfg.id);
            const groups = e.item.getContainer().getParent().getChildren();
            const anchors = groups.filter(item => item.cfg.id);
            // const startPoint = index === 0 ? {x: x ,  y: y } : null;
            // const endPoint = index === 1 ? {x: x, y:  y} : null;
            const origin = index === 0 ? {x: x ,  y: y } : {x:anchors[0].cfg.item.getModel().style.x, y: anchors[0].cfg.item.getModel().style.y };
            const target = index === 1 ? {x: x ,  y: y } : {x:anchors[1].cfg.item.getModel().style.x, y: anchors[1].cfg.item.getModel().style.y };
            this.graph.updateItem(item, {origin: origin, target: target});
            this._resetPoint(e, {x: x, y: y});
        },



        /**
         * 设置点
         * @private
         */
        _setPoint(delegateShape){
            const model = this.target?.get('model');
            if (!this.point) {
                this.point = {
                    x: delegateShape ? delegateShape.getBBox().width : model.style.x,
                    y: delegateShape ? delegateShape.getBBox().height : model.style.y
                }
            }
        },

        /**
         * 重置点
         * @private
         */
        _resetPoint(e, point){
            this.origin = {x:e.x, y: e.y};
            this.point = point;
        },

        /**
         * 设置选中的锚点点位
         * @private
         */
        _setLineAnchorsPosition(){
            const model = this.target.get('model');
            model.style.x = this.point.x;
            model.style.y = this.point.y;
            this.target.getKeyShape().getChildren()[0].attr({
                x: this.point.x,
                y: this.point.y
            });
        },

        _setAnchorsPosition(anchors, groups){
            anchors.forEach(item => {
                const delegateBbox = groups.getBBox();
                const points = this.getAnchorPoints();
                const anchorPoint = points[item.cfg.item._cfg.index];
                item.getChildren()[0].attr({
                    x: delegateBbox.minX + delegateBbox.width * anchorPoint[0],
                    y: delegateBbox.minY + delegateBbox.height * anchorPoint[1]
                })
            })
        },

        getAnchorPoints(){
            return [
                [0, 0],
                [1, 0],
                [1, 1],
                [0, 1],
            ]
        },

        getAnchorLinePoints(){
            return [
                [0, 0],
                [1, 1]
            ]
        },

        onDragEnd(){
            this.point = null;
            this.target = null;
        },

        onDragEnter(){
        },

        onDragLeave(){
        }
    })
}